package com.kaishengit.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Employee;
import com.kaishengit.entity.Goal;
import com.kaishengit.entity.Task;
import com.kaishengit.service.EmployeeService;
import com.kaishengit.service.TaskService;
import com.kaishengit.util.StringUtil;
import com.kaishengit.util.web.ServletUtil;

public class AddTask extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		
		/*HttpSession session = request.getSession();
		Goal goal = (Goal) session.getAttribute("curr_goal");*/
		
		List<Employee> list = new EmployeeService().findAllEmployee();
		
		//request.setAttribute("goal", goal);
		request.setAttribute("list", list);
		ServletUtil.forward(request, response, "task_new");
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html");
		
		String taskname = request.getParameter("taskname");
		String starttime = request.getParameter("starttime");
		String endtime = request.getParameter("endtime");
		String taskdesc = request.getParameter("taskdesc");
		String taskstate = request.getParameter("taskstate");
		String tasklevel = request.getParameter("tasklevel");
		String manager = request.getParameter("manager");
		
		HttpSession session = request.getSession();
		Goal goal = (Goal) session.getAttribute("curr_goal");
		
		Task task = new Task();
		task.setId(StringUtil.getId());
		task.setName(taskname);
		task.setStarttime(starttime);
		task.setEndtime(endtime);
		task.setDesc(taskdesc);
		task.setState(taskstate);
		task.setLevel(tasklevel);
		task.setEmployee_id(manager);
		task.setGoal_id(goal.getId());
		task.setRate(0);
		
		
		TaskService service = new TaskService();
		service.save(task);
		
		ServletUtil.redirect(response, "tasklist.jspx?id=" + goal.getId());
		
	}
}
